const Base = require('./base.js');
module.exports = class extends Base {

	async attend(userid) {
		const self = this;
		let time = (new Date()).setHours(0, 0, 0, 0);
		let attend = await think.model("system/score").where({
			type: 1,
			user_id: userid,
			create_time: ['>', time]
		}).find();
		if(!think.isEmpty(attend)) {
			return {
				data: {

				},
				code: 1
			};
		}
		let score_balance = '';
		const insertId = await this.transaction(async() => {
			await think.model("system/asset").db(self.db()).where({
				user_id: userid
			}).increment('score');
			score_balance = await think.model("system/asset").db(self.db()).where({
				user_id: userid
			}).getField('score', true);
			return await think.model("system/score").db(self.db()).add({
				user_id: userid,
				type: 1,
				create_time: new Date().valueOf(),
				score: 1,
				balance: score_balance,
				status: 1,
				change: 1,
				note: `签到：+1 分`
			});
		});

		if(insertId) {
			return {
				data: {
					score_amount: score_balance,
					score_num: 1
				},
				code: 0
			};
		} else {
			return {
				data: {

				},
				code: 2
			};
		}

	}

};